package com.mystnihon.freeremote.loader.torrent;

import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import ca.benow.transmission.TransmissionClient;
import ca.benow.transmission.model.TorrentStatus;
import ca.benow.transmission.model.TorrentStatus.TorrentField;

import com.mystnihon.freeremote.config.Config;
import com.mystnihon.loader.AbstractAsyncLoader;

public class GetTorrentsLoader extends AbstractAsyncLoader<ArrayList<TorrentStatus>> {
	private static final String TAG = GetTorrentsLoader.class.getSimpleName();
	TransmissionClient mclient;
	int ids[] = null;
	TorrentStatus.TorrentField[] requestedFields = new TorrentField[] { TorrentField.id, TorrentField.name,
			TorrentField.status, TorrentField.percentDone, TorrentField.addedDate, TorrentField.startDate,
			TorrentField.doneDate, TorrentField.files, TorrentField.uploadRatio, TorrentField.peers,
			TorrentField.uploadedEver, TorrentField.downloadedEver,TorrentField.downloadDir, TorrentField.totalSize, TorrentField.sizeWhenDone,TorrentField.eta };

	public GetTorrentsLoader(TransmissionClient client) {
		mclient = client;
	}
	
	public void cancel(){
	    mclient.cancel();
	}

	public GetTorrentsLoader(TransmissionClient client, TorrentStatus.TorrentField[] requestFields, int id) {
		mclient = client;
		requestedFields = requestFields;
		ids = new int[] { id };
	}

	@Override
	protected boolean doLoad(Context context) {
		if (Config.DEBUG_MODE_ENABLED)
			Log.d(TAG, "Loading");
		try {
			setDataObject(mclient.getTorrents(ids, requestedFields));
			if (Config.DEBUG_MODE_ENABLED)
				Log.d(TAG, "Loading return true");
			return true;
		} catch (IOException e) {
			if (Config.ERROR_MODE_ENABLED)
				Log.e(TAG, "Erreur", e);
		}
		if (Config.DEBUG_MODE_ENABLED)
			Log.d(TAG, "Loading return false");
		return false;
	}

}
